#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2007 Google Inc.
#
#
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
import logging
from google.appengine.ext import webapp
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail
import cgi
import wsgiref.handlers
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.api import urlfetch
import urllib # Used to unescape URL parameters.
#import settings

try:
  from xml.etree import ElementTree
except ImportError:
  from elementtree import ElementTree
import gdata.calendar.service#Google data calender
import gdata.service
import atom.service
import gdata.calendar
import atom
import getopt
import sys
import string
import time
import datetime
from google.appengine.api import urlfetch
#import ssl

def sendsms2me(msg):
    nm=msg.replace(' ','%20')
    hkurl='http://palm2you.appspot.com/xsms?xsms='+nm  
    req=urlfetch.fetch(hkurl,method=urlfetch.GET,headers={},allow_truncated=False,follow_redirects=False)
#    content=str(req.content.decode("utf8"))
class Xhkcheck(webapp.RequestHandler):
    def get(self):
        self.curtime=str(datetime.datetime.now())
#        print('start at:',datetime.datetime.now())
    #    sendsms2me('start at:'+str(datetime.now()))
    #    while 1:
        visaid=''
        visatag=''
        
        visaid=self.request.get('visaid')
        visatag=self.request.get('visatag')
        if not visaid:return
        self.checkonce(visaid,visatag)
#        self.checkonce('030800029287733','lwy')
#        self.checkonce('030800029287940','lrr')
#        self.checkonce('030800029287733','lwy')
    def checkonce(self,aid,whoami):    
        checkurl='http://www.gdcrj.com/wsyw/tcustomer/tcustomer.do?&method=find&applyid='+aid
#        self.response.out.write(self.curtime+'--'+checkurl+'---<br>')
        req=urlfetch.fetch(checkurl,method=urlfetch.GET,headers={"Referer":'http://www.gdcrj.com/wsfwdt/wssq/'},allow_truncated=False,follow_redirects=False)
        content=req.content.decode("utf8")
#        self.response.out.write(self.curtime+'--'+content.encode('utf8')+'---<br>')

        i=content.find(aid)
        if i<0:
            self.response.out.write(self.curtime+'--i='+str(i)+'---查询失败<br>')
            return
        p=content.find('</td>',i)
        if p<0:
            self.response.out.write(self.curtime+'--p='+str(p)+'---<br>')
            return
        ns=content[i:p].encode('utf8')

#        self.response.out.write(self.curtime+'--'+ns+'---<br>')
        tag='您的申请正在受理中'
        tag2='您的申请已受理'
#        self.response.out.write(self.curtime+'--'+tag+'---not ok<br>')
        i=ns.find(tag)
        i2=ns.find(tag2)
        if i<0 and i2<0:
            self.response.out.write(self.curtime+'--'+whoami+'---hk visa is ok<br>')
            sendsms2me(whoami + 'hk visa is ok')
        else:
            self.response.out.write(self.curtime+'--'+whoami+'---not ok<br>')



def main():
    application = webapp.WSGIApplication([('/hkvisacheck',Xhkcheck)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()